package reco.frame.demo.adapter;

import java.util.ArrayList;
import java.util.List;

import reco.frame.demo.R;
import reco.frame.demo.entity.AppInfo;
import reco.frame.tv.view.TvImageView;
import reco.frame.tv.view.TvRelativeLayout;
import reco.frame.tv.view.TvVerticalPager;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class TvPageAdapter extends PagerAdapter {
	
	private final static String TAG="TvPageAdapter";
	private List<List<AppInfo>> pageList;
	private Context context;
	private int width, height, space, margin, padding;
	private LayoutInflater mInflater;
	private int marginTop;
	private int column = 4;

	public TvPageAdapter(Context context, List<AppInfo> itemList) {

		this.context = context;
		this.mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.width = (int) context.getResources().getDimension(R.dimen.px546);
		this.height = (int) context.getResources().getDimension(R.dimen.px320);
		this.margin = (int) context.getResources().getDimension(R.dimen.px40);
		this.space = (int) context.getResources().getDimension(R.dimen.px20);
		init(itemList);

	}

	private void init(List<AppInfo> itemList) {
		pageList = new ArrayList<List<AppInfo>>();
		List<AppInfo> page = new ArrayList<AppInfo>();
		for (int i = 0; i < itemList.size(); i++) {

			AppInfo app = itemList.get(i);
			page.add(app);

			if (page.size() >= column * 2) {
				pageList.add(page);
				page = new ArrayList<AppInfo>();

			} else if (i == (itemList.size() - 1)) {
				pageList.add(page);
				break;
			}

		}
	}

	@Override
	public int getCount() {
		return pageList.size();
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {

		View pageView = null;
		try {
			pageView = buildView(pageList.get(position), position + 1);
			// ((TvVerticalPager) container).addView(pageView);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return pageView;
	}

	@Override
	public void destroyItem(View container, int position, Object object) {
		((TvVerticalPager) container).removeView((View) object);
	}
	AnimatorSet animatorSet;
	private View buildView(List<AppInfo> appList, final int pageNum) {
		
		Gallery page = (Gallery) mInflater.inflate(R.layout.item_page, null);
		
		page.setAdapter(new GalleryAdapter(context));
		page.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> root, View item,
					int position, long id) {
				
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> root) {
				
			}
		});
		page.setSelection(7);

		return page;
	}

}
